package com.arch.dev.client.view;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.CatagoryVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.tractionsoftware.gwt.user.client.ui.UTCDateBox;

public class FormView extends Composite {

	private static FormViewUiBinder uiBinder = GWT
			.create(FormViewUiBinder.class);

	@UiField(provided = true)
	FlexTable formTable;

	TextArea descTextBox;

	TextBox costTextBox;

	UTCDateBox dateBox;

	// Button saveButton;

	ListBox catagoryListBox;

	@UiField
	Button cancelButton;

	public Button getCancelButton() {
		return cancelButton;
	}

	public void setCancelButton(Button cancelButton) {
		this.cancelButton = cancelButton;
	}

	@UiField
	Button saveButton;

	interface FormViewUiBinder extends UiBinder<Widget, FormView> {
	}

	public FormView() {

		init();

		initWidget(uiBinder.createAndBindUi(this));
	}

	private void init() {
		formTable = new FlexTable();

		formTable.setCellSpacing(6);

		FlexCellFormatter cellFormatter = formTable.getFlexCellFormatter();

		// Create a DateBox

		catagoryListBox = new ListBox(false);

		catagoryListBox.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent event) {

				descTextBox.setText(getSelectedCatagoryName());

			}

		});

		// populateCatagoryListBox();

		DateTimeFormat dateFormat = DateTimeFormat
				.getFormat(PredefinedFormat.DATE_LONG);
		DatePicker datePicker = new DatePicker();
		dateBox = new UTCDateBox(datePicker, -1,
				new DateBox.DefaultFormat(dateFormat));

		// dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
		Date currentDate = new Date();
		dateBox.setValue(UTCDateBox.date2utc(currentDate));		

		//dateBox.setValue();

		// saveButton = new Button("Save");

		/*
		 * saveButton.addClickHandler(new ClickHandler() {
		 * 
		 * public void onClick(ClickEvent event) {
		 * 
		 * BudgetController.getInstance().persistValues(
		 * 
		 * FormView.this, populateVo());
		 * 
		 * }
		 * 
		 * });
		 */

		// Add a title to the form

		// formTable.setHTML(0, 0, "Enter Details");

		// cellFormatter.setColSpan(0, 0, 2);

		// cellFormatter.setHorizontalAlignment(0, 0,

		// HasHorizontalAlignment.ALIGN_CENTER);

		descTextBox = new TextArea();

		descTextBox.setVisibleLines(1);
		descTextBox.setWidth("90%");

		costTextBox = new TextBox();
		costTextBox.setValue("0.0");

		// Add some standard form options

		formTable.setHTML(1, 0, "Description :");

		formTable.setWidget(1, 1, descTextBox);

		formTable.setHTML(1, 2, "Date :");

		formTable.setWidget(1, 3, dateBox);

		formTable.setHTML(2, 0, "Catagory :");

		formTable.setWidget(2, 1, catagoryListBox);

		formTable.setHTML(2, 2, "Cost :");

		formTable.setWidget(2, 3, costTextBox);

		// formTable.setWidget(3, 1, saveButton);
	}

	public void populateCatagoryListBox() {

		BudgetController.getInstance().getAllCatagories(this);

	}

	public void populateCatagoryListBox(Widget view) {

		BudgetController.getInstance().getAllCatagories(view);

	}

	public void populateCatagoryListBox(List<CatagoryVO> catagoryVOs) {
		catagoryListBox.clear();
		for (CatagoryVO catagoryVO : catagoryVOs) {

			catagoryListBox.addItem(catagoryVO.getName(), catagoryVO.getId()
					.toString());

		}

		descTextBox.setText(getSelectedCatagoryName());
		isCatagoryListBoxPopulated = true;

	}

	private String getSelectedCatagoryName() {
		int index = catagoryListBox.getSelectedIndex();

		return index != -1 ? catagoryListBox.getItemText(index) : "";

	}

	public BudgetVO populateVo() {

		BudgetVO budgetVO = new BudgetVO();

		if (bdgVO != null && bdgVO.getId() != null) {
			budgetVO.setId(bdgVO.getId());
		}
		if (bdgVO != null && bdgVO.getCreatedDate() != null) {
			budgetVO.setCreatedDate(bdgVO.getCreatedDate());
		}

		if (bdgVO != null && bdgVO.getLastUpdated() != null) {
			budgetVO.setLastUpdated(bdgVO.getLastUpdated());
		}

		budgetVO.setTitle(descTextBox.getText());

		budgetVO.setDate(new Date(dateBox.getValue()));

		CatagoryVO catagoryVO = new CatagoryVO();

		Long id = Long.parseLong(catagoryListBox.getValue(catagoryListBox
				.getSelectedIndex()));

		catagoryVO.setId(id);

		catagoryVO.setName(getSelectedCatagoryName());

		budgetVO.setCatagoryVO(catagoryVO);

		budgetVO.setCost(BigDecimal.valueOf(Double.parseDouble(costTextBox
				.getText())));

		return budgetVO;

	}

	public void saveButtonAddClickHandler(ClickHandler handler) {
		saveButton.addClickHandler(handler);
	}

	public void resetWidgets() {
		descTextBox.setText(getSelectedCatagoryName());
		descTextBox.setFocus(true);
	}

	private BudgetVO bdgVO = null;

	public void populateFields(BudgetVO budgetVO) {

		bdgVO = budgetVO;

		descTextBox.setText(budgetVO.getTitle());

		dateBox.setValue(budgetVO.getDate().getTime());

		costTextBox.setText(budgetVO.getCost().toString());

		catagoryListBox.setSelectedIndex(getIndexOfCatagoryFromListBox(budgetVO
				.getCatagoryVO()));

	}

	boolean isCatagoryListBoxPopulated = false;

	public int getIndexOfCatagoryFromListBox(CatagoryVO catagoryVO) {

		while (true) {

			if (isCatagoryListBoxPopulated) {

				for (int i = 0; i < catagoryListBox.getItemCount(); i++) {

					if (catagoryListBox.getValue(i).equals(
							catagoryVO.getId().toString())) {

						return i;

					}

				}

				break;

			}

		}

		return -1;

	}

}
